package com.example.homework;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.ContentResolver;
import android.database.Cursor;
import android.media.Image;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    //媒体播放器
    private MediaPlayer mediaPlayer;
    //存放歌曲的list集合
    private List<Song> list = new ArrayList<>();

    private ListView listItem;
    private SeekBar seekBar;
    private Button style;
    private ImageView back;
    private ImageView begin;
    private ImageView pause;
    private ImageView next;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listItem = (ListView) findViewById(R.id.list_item);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        style = (Button) findViewById(R.id.style);
        back = (ImageView) findViewById(R.id.back);
        begin = (ImageView) findViewById(R.id.begin);
        pause = (ImageView) findViewById(R.id.pause);
        next = (ImageView) findViewById(R.id.next);

        //初始化媒体播放器
        mediaPlayer = new MediaPlayer();

        //请求授权
        //动态获取 读取SD卡的权限
        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},100);

        //获取内容接收者
        ContentResolver contentResolver = getContentResolver();

        //调用查询方法
        Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);

        if (cursor != null){
            while (cursor.moveToNext()){
                //创建Song对象
                Song song = new Song();

                //赋值
                song.setImg("");
                song.setMname(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));//歌名
                song.setPname(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));//歌手
                song.setTime(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)));//时长
                song.setPath(cursor.getColumnName(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));//地址

                //将歌曲添加到list集合中
                list.add(song);
            }

            //释放资源
            cursor.close();
        }else {
            Toast.makeText(this, "没有找到文件", Toast.LENGTH_SHORT).show();
        }

        //开启适配器
        MyAdapter myAdapter = new MyAdapter();
        listItem.setAdapter(myAdapter);

        begin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                begin.setVisibility(View.GONE);
                pause.setVisibility(View.VISIBLE);
            }
        });
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            VH vh;
            if (convertView == null){
                vh = new VH();

                convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout,null);

                vh.img = convertView.findViewById(R.id.img);
                vh.mname = convertView.findViewById(R.id.mname);
                vh.pname = convertView.findViewById(R.id.pname);
                vh.time = convertView.findViewById(R.id.time);

                convertView.setTag(vh);
            }else {
                vh = (VH) convertView.getTag();
            }

            vh.img.setImageResource(R.mipmap.ic_launcher_round);
            vh.mname.setText(list.get(position).getMname());
            vh.pname.setText(list.get(position).getPname());
            vh.time.setText(list.get(position).getTime());

            return convertView;
        }
        class VH{
            ImageView img;
            TextView mname;
            TextView pname;
            TextView time;
        }
    }
}
